/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define PLUGIN_NAME		"Simple announce"
#define PLUGIN_AUTHOR		"Blaize"
#define PLUGIN_DESC		"Show presentation message"
#define PLUGIN_VERSION		"0.1"
#define PLUGIN_FILENAME		"l4d2_simple_announce"

#include <sourcemod>
#include <sdktools>
#include <l4d_stocks>

#include "macros.sp"
#include "helpers.sp"

new user_id_already_connected[MAXCLIENTS];
new bool:round_is_started = false;

new started_time = 0;

new last_min = 0;
new last_sec = 0;

new bool:is_first_round;

public OnPluginStart()
{

	for(new i=0; i < MAXCLIENTS; i++)
	{
		user_id_already_connected[i] = -1;
	}

	HookEvent("player_death", player_is_dead, EventHookMode_Post);
	HookEvent("versus_round_start",Event_RoundStart);
	HookEvent("round_end", Event_RoundEnd);
	
	is_first_round = true;

	RegConsoleCmd("lol", lol_at);
	RegConsoleCmd("me", me_cmd);
}

public OnMapEnd()
{
	is_first_round = true;
}

public Action:display_ip(Handle:timer, any:junk)
{
	
	PrintToChatAll("\x03Free Arena 20Vs Arcade\x01 - IP : \x04176.31.110.188:27015");
	PrintToChatAll("\x01Join us : \x03http://steamcommunity.com/groups/left4freedom\x01");

}
public Action:me_cmd(client, args)
{
	decl String:full[128];
	GetCmdArgString(full, sizeof(full));

	if(IsValidClient(client))
	{
		PrintToChatAll("\x03%N\x01 %s", client, full);
	}

	return Plugin_Handled;
}

public Action:lol_at(client, args)
{
	decl String:full[128];
	GetCmdArgString(full, sizeof(full));
	decl String:name[128];
	decl target_list[MAXCLIENTS], bool:tn_is_ml;

	if(IsValidClient(client))
	{

		if(strcmp(full,"@all") == 0)
		{
			PrintToChatAll("\x03%N\x01 is laughing at \x03server\x01", client, name);
			return Plugin_Handled;
		}

		if(strcmp(full,"@bots") == 0)
		{
			PrintToChatAll("\x03%N\x01 is laughing at \x03bots\x01", client, name);
			return Plugin_Handled;
		}
		
		if(strcmp(full,"@me") == 0)
		{
			PrintToChatAll("\x03%N\x01 is laughing at \x03himself\x01", client, name);
			return Plugin_Handled;
		}

		new target_count = ProcessTargetString(
			full,
			0,
			target_list,
			MAXCLIENTS,
			COMMAND_FILTER_NO_MULTI,
			name,
			sizeof(name),
			tn_is_ml);
		
		if(!tn_is_ml && target_count == 1)
		{		
			PrintToChat(target_list[0], "\x05%N\x01 is laughing at you :)", client);
			PrintToChat(client, "\x01You laugh at \x05%N\x01 :)", target_list[0]);
		}
	}

	return Plugin_Handled;
}
public Event_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	round_is_started = true;
	started_time = GetTime();
	if(is_first_round)
		CreateTimer(450.0, display_ip, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
}

public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
{
	if(round_is_started)
	{
		new finaltime = GetTime() - started_time;
		new min = finaltime / 60;
		new sec = finaltime % 60;

		if(is_first_round)
		{
			is_first_round = false;
			PrintToChatAll("\x01Round duration : \x03%02d:%02d", min, sec);
			last_min = min;
			last_sec = sec;
		}
		else
		{
			PrintToChatAll("\x01Round duration : \x03%02d:%02d\x01 (first team: \x03%02d:%02d\x01)", min, sec, last_min, last_sec);
		}
	}
	round_is_started = false;
}

public OnClientPutInServer(Client)
{
	if (IsValidClient(Client) && !IsFakeClient(Client))
	{
		PrintToChat(Client, "\x03Free Arena 20Vs Arcade\x01 - IP : \x04176.31.110.188:27015");
		PrintToChat(Client, "\x01Join us : \x03http://steamcommunity.com/groups/left4freedom\x01");
	}
	
}
public OnClientConnected(Client)
{

	if (IsValidClientIndex(Client) && !IsFakeClient(Client) && !already_connected(Client))
	{
		add_client(Client);
		PrintToChatAll("\x03%N\x01 has joined the game!", Client);
	}
}


/* clean the last class when player leave, for the next player */
public OnClientDisconnect(Client)
{
	if (IsValidHumanClient(Client))
	{
		remove_client(Client);
		PrintToChatAll("\x03%N\x01 has left the game.", Client);
	}
}

public add_client(Client)
{
	new userID = GetClientUserId(Client);
	for(new i=0; i < MAXCLIENTS; i++)
	{
		if(user_id_already_connected[i] == -1) // found slot
		{
			user_id_already_connected[i] = userID;
			return;
		}
	}
}

public remove_client(Client)
{
	new userID = GetClientUserId(Client);
	for(new i=0; i < MAXCLIENTS; i++)
	{
		if(user_id_already_connected[i] == userID) // found slot
		{
			user_id_already_connected[i] = -1;
			return;
		}
	}

}

public bool:already_connected(Client)
{
	new userID = GetClientUserId(Client);
	for(new i=0; i < MAXCLIENTS; i++)
	{
		if(user_id_already_connected[i] == userID) // found slot
		{
			return true;
		}
	}
	return false;
}

public count_number_alive()
{
	new count = 0;
	FOR_EACH_ALIVE_SURVIVOR(client)
	{
		count++;
	}

	return count;
}

public Action:player_is_dead(Handle:event, const String:name[], bool:dontBroadcast)
{
	
	if(!round_is_started)
		return Plugin_Continue;

	new target = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if(IsValidSurvivor(target))
	{
		new count = count_number_alive();
		new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));

		if(IsValidClient(attacker))
		{
			if(attacker != target)
				PrintToChatAll("\x03%N\x01 has died. (killed by %N) +%d",target, attacker, count);    
			else
			PrintToChatAll("\x03%N\x01 has died. (by himself) +%d",target, count);
		}
		else
		{
			PrintToChatAll("\x03%N\x01 has died. +%d",target, count);
		}
	}

	return Plugin_Continue;
}